package com.qxzn.app.page.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.qxzn.app.page.R;

public class MaxHeightRecyclerView extends LRecyclerView {
    private int mMaxHeight;

    public MaxHeightRecyclerView(Context context) {
        super(context);
    }

    public MaxHeightRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public MaxHeightRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.MaxHeightRecyclerView, defStyle, 0);
        mMaxHeight = a.getLayoutDimension(R.styleable.MaxHeightRecyclerView_recycler_max_height, mMaxHeight);
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        if (mMaxHeight > 0) {
            heightSpec = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST);
        }
        super.onMeasure(widthSpec, heightSpec);
    }
}
